package com.qzsoft.system.modules.sysstation.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.system.modules.sysstation.entity.SysStationAsphaltMaterialDetailsEntity;
import com.qzsoft.system.modules.sysstation.entity.SysStationAsphaltMaterialEntity;
import com.qzsoft.system.modules.sysstation.entity.SysStationEntity;
import com.qzsoft.system.modules.sysstation.enums.SysStationType;
import com.qzsoft.system.modules.sysstation.mapper.SysStationAsphaltMaterialMapper;
import com.qzsoft.system.modules.sysstation.service.SysStationAsphaltMaterialDetailsService;
import com.qzsoft.system.modules.sysstation.service.SysStationAsphaltMaterialService;
import com.qzsoft.system.modules.sysstation.service.SysStationService;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;

/**
 * @ClassName SysStationAsphaltMaterialServiceImpl
 * @Description 沥青物料设置业务接口实现
 * @author linhuawei
 * @since 2021-11-26
 */
@Service
public class SysStationAsphaltMaterialServiceImpl
        extends ServiceImpl<SysStationAsphaltMaterialMapper, SysStationAsphaltMaterialEntity>
        implements SysStationAsphaltMaterialService {

    @Autowired
    private SysStationAsphaltMaterialDetailsService sysStationAsphaltMaterialDetailsService;

    @Autowired
    private SysStationService sysStationService;

    @Override
    public SysStationAsphaltMaterialEntity getById(Serializable id) {
        // 获取配置信息
        SysStationAsphaltMaterialEntity entity = baseMapper.selectById(id);
        if (null == entity) {
            return null;
        }

        // 获取物料信息
        QueryWrapper<SysStationAsphaltMaterialDetailsEntity> queryWrapper = new QueryWrapper<SysStationAsphaltMaterialDetailsEntity>();
        queryWrapper.eq("station_id", id);
        queryWrapper.orderByAsc("size");
        List<SysStationAsphaltMaterialDetailsEntity> details = sysStationAsphaltMaterialDetailsService
                .list(queryWrapper);
        entity.setDetails(details);

        // 返回结果
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(SysStationAsphaltMaterialEntity entity) {
        // 获取拌合楼信息
        SysStationEntity station = sysStationService.getById(entity.getId());
        if (null == station) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "拌合楼不存在");
        }

        // 判断是否沥青拌合楼，如果时沥青拌合楼需要判断沥青矿粉价格
        if (SysStationType.ASPHALT.getValue().equals(station.getType())) {
            if (null == entity.getAsphaltPrice()) {
                RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "沥青价格不可为空");
            }
            if (null == entity.getPowderPrice()) {
                RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "矿粉价格不可为空");
            }
        }

        // 判断是否存在
        SysStationAsphaltMaterialEntity temp = baseMapper.selectById(entity.getId());
        if (null == temp) {
            save(entity);
        } else {
            updateById(entity);

            // 删除设置详情
            sysStationAsphaltMaterialDetailsService.deleteByStationId(entity.getId());
        }

        // 添加配置详情
        List<SysStationAsphaltMaterialDetailsEntity> details = entity.getDetails();
        if (ObjectUtils.isNotEmpty(details)) {
            details.forEach(item -> {
                item.setStationId(entity.getId());
            });
            sysStationAsphaltMaterialDetailsService.saveBatch(details);
        }

        // 更新拌合楼配置
        if (null == station.getMaterialConfig() || 1 != station.getMaterialConfig()) {
            sysStationService.updateMaterialConfig(entity.getId(), 1);
        }

        // 返回结果
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        // 删除设置详情
        sysStationAsphaltMaterialDetailsService.deleteByStationId(id);

        // 删除设置信息
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByStationId(Collection<? extends Serializable> stationIdList) {
        // 判断是否需要删除
        if (ObjectUtils.isEmpty(stationIdList)) {
            return;
        }

        // 删除设置详情
        sysStationAsphaltMaterialDetailsService.deleteByStationId(stationIdList);

        // 删除物料信息
        QueryWrapper<SysStationAsphaltMaterialEntity> queryWrapper = new QueryWrapper<SysStationAsphaltMaterialEntity>();
        queryWrapper.in("station_id", stationIdList);
        baseMapper.delete(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByStationId(Serializable stationId) {
        // 删除设置详情
        sysStationAsphaltMaterialDetailsService.deleteByStationId(stationId);

        // 删除物料信息
        QueryWrapper<SysStationAsphaltMaterialEntity> queryWrapper = new QueryWrapper<SysStationAsphaltMaterialEntity>();
        queryWrapper.eq("id", stationId);
        baseMapper.delete(queryWrapper);
    }

}
